#Define margins
par(mar=c(1,1,1,1))

path = "/Users/thiagoscott/Dropbox/Juizes Brasil/Paper_Selection/submissions/Restat_Submission/acceptance/materials/"
paper_figures = paste0(path,'/output/figures/')
  
#Loads Stata dataset
dataset_estimate <- fread(paste0(path,"temp/Intermed_FE_R.csv")) 


###### Figure A4: Construction of connected sets in the data (Rond?nia - May 2013) ####

### Graph Amapa and Rondonia to show disjoint connected sets ###
graph = subset(dataset_estimate, (State == "AP" | State == "RO"), 
               c(Judge_idP, Court_idP2, Judge, Court_name))

#Create incidence matrix to graph 
links2 = table(cbind.data.frame(Judge=graph$Judge_idP,Court=graph$Court_idP2))
net2 <- graph_from_incidence_matrix(links2)

table(V(net2)$type)
V(net2)$shape <- c("square", "circle")[V(net2)$type+1]
V(net2)$label <- ""
V(net2)$label.cex=.5
V(net2)$label.font=.5
V(net2)$color = c("white",29)[V(net2)$type+1]

l <- layout_nicely(net2)

plot(net2, vertex.label.color="black", vertex.label.dist=1,
     vertex.size=(1+V(net2)$type)*1.5, layout = l, 
     main = 'Rondonia (L) & Amapa (R) States (2009-2015)')
dev.copy(pdf,paste0(paper_figures,'CS_States.pdf'))
dev.off()

##GRAPH SPECIFIC
#Subset small part to show network of RO (2009)
graph = subset(dataset_estimate, (State == 'RO') & (year >= 2009 & year <= 2009 ) , 
               c(Judge_idP, Court_idP2, Judge, Court_name))


#Create incidence matrix to graph 
links2 = table(cbind.data.frame(Judge=graph$Judge_idP,Court=graph$Court_idP2))
net2 <- graph_from_incidence_matrix(links2)

V(net2)$shape <- c("square", "circle")[V(net2)$type+1]
V(net2)$label <- ""
V(net2)$label.cex=.5
V(net2)$label.font=.5
V(net2)$color = c("white",29)[V(net2)$type+1]

l <- layout_nicely(net2)

plot(net2, vertex.size=(1+V(net2)$type)*2.5, layout = l, main = 'Rondonia (2009) ')
dev.copy(pdf,paste0(paper_figures,'/CS_RO_9.pdf'))
dev.off()

#Subset small part to show network of RO (2009-2010)
graph = subset(dataset_estimate, (State == 'RO') & (year >= 2009 & year <= 2010 ) , 
               c(Judge_idP, Court_idP2, Judge, Court_name))

#Create incidence matrix to graph 
links2 = table(cbind.data.frame(Judge=graph$Judge_idP,Court=graph$Court_idP2))
net2 <- graph_from_incidence_matrix(links2)

V(net2)$shape <- c("square", "circle")[V(net2)$type+1]
V(net2)$label <- ""
V(net2)$label.cex=.5
V(net2)$label.font=.5
V(net2)$color = c("white",29)[V(net2)$type+1]

l <- layout_nicely(net2)

plot(net2, vertex.size=(1+V(net2)$type)*2.5, layout = l, main = 'Rondonia (2009 - 2010) ')
dev.copy(pdf,paste0(paper_figures,'CS_RO_910.pdf'))
dev.off()

#Subset small part to show network of RO (2009-2013)
graph = subset(dataset_estimate, (State == 'RO') & (year >= 2009 & year <= 2011 ) , 
               c(Judge_idP, Court_idP2, Judge, Court_name))

#Create incidence matrix to graph 
links2 = table(cbind.data.frame(Judge=graph$Judge_idP,Court=graph$Court_idP2))
net2 <- graph_from_incidence_matrix(links2)

V(net2)$shape <- c("square", "circle")[V(net2)$type+1]
V(net2)$label <- ""
V(net2)$label.cex=.5
V(net2)$label.font=.5
V(net2)$color = c("white",29)[V(net2)$type+1]

l <- layout_nicely(net2)

plot(net2, vertex.size=(1+V(net2)$type)*2.5, layout = l, main = 'Rondonia (2009 - 2011) ')
legend(x=-1.5, y=-1.1, c("Judges", "Courts"), pch=c(22,21),
       col="#777777", pt.bg=c("white",29), pt.cex=2, cex=.8, bty="n", ncol=1)
dev.copy(pdf,paste0(paper_figures,'CS_RO_911.pdf'))
dev.off()


###### Figure A5: Visualizing connected sets in the data #####

### Showing specific connections within states ###
graph = subset(dataset_estimate, (Court_idP2 == -328) & (year == 2013) & (month == 5) , 
               c(Judge_idP, Court_idP2, Judge, Court_name))

#Create incidence matrix to graph 
links2 = table(cbind.data.frame(Judge=graph$Judge_idP,Court=graph$Court_idP2))
net2 <- graph_from_incidence_matrix(links2)
V(net2)$name

#need to create Edges graph, describing edges (judges and courts)
judge = subset(graph[!duplicated(graph$Judge_idP),], ,c('Judge_idP', 'Judge'))
judge = judge[order(judge$Judge_idP),]
court = subset(graph[!duplicated(graph$Court_idP2),], ,c('Court_idP2', 'Court_name'))
court = court[order(court$Court_idP2),]

names(judge) = c('id','name')
names(court) = c('id','name')

longform = rbind(judge, court)

V(net2)$shape <- c("square", "circle")[V(net2)$type+1]
V(net2)$label <- ""
V(net2)$label <- longform$name
V(net2)$label.cex=1
V(net2)$label.font=.5
V(net2)$color = c("white",29)[V(net2)$type+1]

l <- layout_with_fr(net2)

plot(net2, vertex.size=(1+V(net2)$type)*4, vertex.label.dist=2, layout = l)
dev.copy(pdf,paste0(paper_figures,'RO_Group1.pdf'))
dev.off()


## Show one specific set of connections
graph = subset(dataset_estimate, (Court_idP2 == -328 | Judge_idP == 5142 | Judge_idP == 1535 | Judge_idP == 3239) & 
                 (year == 2013) & (month == 5)  , c(Judge_idP, Court_idP2, Judge, Court_name))

#Create incidence matrix to graph 
links2 = table(cbind.data.frame(Judge=graph$Judge_idP,Court=graph$Court_idP2))
net2 <- graph_from_incidence_matrix(links2)
V(net2)$name

#need to create Edges graph, describing edges (judges and courts)
judge = subset(graph[!duplicated(graph$Judge_idP),], ,c('Judge_idP', 'Judge'))
judge = judge[order(judge$Judge_idP),]
court = subset(graph[!duplicated(graph$Court_idP2),], ,c('Court_idP2', 'Court_name'))
court = court[order(court$Court_idP2),]

names(judge) = c('id','name')
names(court) = c('id','name')

longform = rbind(judge, court)

V(net2)$shape <- c("square", "circle")[V(net2)$type+1]
V(net2)$label <- ""
V(net2)$label <- longform$name
V(net2)$label.cex=1
V(net2)$label.font=.5
V(net2)$color = c("white",29)[V(net2)$type+1]

l <- layout_with_fr(net2)

plot(net2, vertex.size=(1+V(net2)$type)*4, vertex.label.dist=2, layout = l)
dev.copy(pdf,paste0(paper_figures,'RO_Group2.pdf'))
dev.off()

###

## Show one specific set of connections
graph = subset(dataset_estimate, (Court_idP2 == -328 |Court_idP2 == -5812 |Court_idP2 == -5149 | Judge_idP == 5142 | Judge_idP == 1535 | Judge_idP == 3239) & 
                 (year == 2013) & (month == 5)  , c(Judge_idP, Court_idP2, Judge, Court_name))

#Create incidence matrix to graph 
links2 = table(cbind.data.frame(Judge=graph$Judge_idP,Court=graph$Court_idP2))
net2 <- graph_from_incidence_matrix(links2)
V(net2)$name

#need to create Edges graph, describing edges (judges and courts)
judge = subset(graph[!duplicated(graph$Judge_idP),], ,c('Judge_idP', 'Judge'))
judge = judge[order(judge$Judge_idP),]
court = subset(graph[!duplicated(graph$Court_idP2),], ,c('Court_idP2', 'Court_name'))
court = court[order(court$Court_idP2),]

names(judge) = c('id','name')
names(court) = c('id','name')

longform = rbind(judge, court)

V(net2)$shape <- c("square", "circle")[V(net2)$type+1]
V(net2)$label <- ""
V(net2)$label <- longform$name
V(net2)$label.cex=1
V(net2)$label.font=.5
V(net2)$color = c("white",29)[V(net2)$type+1]

l <- layout_with_fr(net2)

plot(net2, vertex.size=(1+V(net2)$type)*4, vertex.label.dist=2, layout = l)
dev.copy(pdf,paste0(paper_figures,'RO_Group3.pdf'))
dev.off()

